﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Web.Mvc;

namespace MyStore.PresentationModel
{
    public class RestRoute : RouteBase
    {
        List<Route> _internalRoutes = new List<Route>();
        public string Resource { get; private set; }

        public RestRoute(string resource)
        {
            Resource = resource;
            MapRoute(resource, "index", "GET", null);
            MapRoute(resource, "create", "POST", null);
            MapRoute(resource + "/new", "newitem", "GET", null);
            MapRoute(resource + "/{id}", "show", "GET", new { id = @"\d+" });
            MapRoute(resource + "/{id}", "update", "PUT", new { id = @"\d+" });
            MapRoute(resource + "/{id}", "delete", "DELETE", new { id = @"\d+" });
            MapRoute(resource + "/{id}/edit", "edit", "GET", new { id = @"\d+" });
        }

        private void MapRoute(string url, string actionName, string httpMethod, object constraints)
        {
            RouteValueDictionary constraintsDictionary;

            if (constraints != null)
            {
                constraintsDictionary = new RouteValueDictionary(constraints);
            }
            else
            {
                constraintsDictionary = new RouteValueDictionary();
            }
            constraintsDictionary.Add("httpMethod", new HttpMethodConstraint(httpMethod));

            _internalRoutes.Add(new Route(url, new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = Resource, action = actionName }),
                Constraints = constraintsDictionary
            });
        }


        public override RouteData GetRouteData(System.Web.HttpContextBase httpContext)
        {
            foreach (var route in this._internalRoutes)
            {
                var rvd = route.GetRouteData(httpContext);
                if (rvd != null)
                    return rvd;
            }
            return null;
        }

        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            foreach (var route in this._internalRoutes)
            {
                var rpd = route.GetVirtualPath(requestContext, values);
                if (rpd != null)
                    return rpd;
            }
            return null;
        }
    }
}
